/*
    BibleNotebook - A bible reader software
    Copyright (C) 2014  Williams Fernando Lopez

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
 */

package com.simplebible;

import com.simplebible.bible.BibleReader;
import wl.util.TextResourcesUtil;
import wl.util.Utils;
import java.awt.KeyboardFocusManager;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import com.simplebible.objects.Preferences;
import com.simplebible.ui.KeyDispatcher;
import com.simplebible.ui.components.DialogInstallBibles;
import com.simplebible.ui.components.MainPanel;

/**
 *
 * @author Williams Lopez
 */
public class Main {


    private static JFrame frame;
    private static MainPanel panel;
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

        Preferences.getInst();

        try {
            TextResourcesUtil.loadResource(Main.class, "/simplebible/resources/text.properties");
        } catch (Exception e) {
            e.printStackTrace();
        }
        

        try {
            // Set System L&F
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

        } catch (UnsupportedLookAndFeelException e) {
            Utils.printToLog("No se puede arrancar el look and feel", e, Preferences.LOG_LEVEL_ERROR);
        } catch (ClassNotFoundException e) {
            Utils.printToLog("No se puede arrancar el look and feel", e, Preferences.LOG_LEVEL_ERROR);
        } catch (InstantiationException e) {
            Utils.printToLog("No se puede arrancar el look and feel", e, Preferences.LOG_LEVEL_ERROR);
        } catch (IllegalAccessException e) {
            Utils.printToLog("No se puede arrancar el look and feel", e, Preferences.LOG_LEVEL_ERROR);
        }



        panel = new MainPanel();


        try {
            KeyboardFocusManager manager =
                    KeyboardFocusManager.getCurrentKeyboardFocusManager();
            manager.addKeyEventDispatcher(new KeyDispatcher(panel));
        } catch (Exception e) {
            Utils.printToLog("No se puede arrancar el keydispatcher", e, Preferences.LOG_LEVEL_ERROR);
        }



        frame = getFrame();
        panel.setMainFrame(frame);
        frame.pack();
        frame.addWindowListener(new WindowListener() {
            public void windowOpened(WindowEvent e) {}
            public void windowClosing(WindowEvent e) {}
            public void windowClosed(WindowEvent e) {
                panel.finish();
            }
            public void windowIconified(WindowEvent e) {}
            public void windowDeiconified(WindowEvent e) {}
            public void windowActivated(WindowEvent e) {}
            public void windowDeactivated(WindowEvent e) {}
        });
        frame.setVisible(true);

        try{
            if(BibleReader.getVersions().isEmpty()){
                new DialogInstallBibles(frame, false).setVisible(true);
            }
        }catch(Exception e){
            Utils.printToLog("Error al validar el numero de versiones", e, Preferences.LOG_LEVEL_ERROR);
        }


    }

    public static void refreshUI(){
        panel.refresh();
    }


    public static JFrame getMainFrame(){
        return frame;
    }


    private static JFrame getFrame(){

        frame= new JFrame();

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setContentPane(panel);
        frame.setLocationByPlatform(true);
        frame.setTitle(TextResourcesUtil.getText("app.title"));
        frame.setIconImage(new ImageIcon(Main.class.getResource("/simplebible/ui/img/icon.png")).getImage());
        //frame.setUndecorated(true);
        //frame.setDefaultLookAndFeelDecorated(true);

        //frame.setUndecorated(true);
        frame.addWindowListener(new WindowListener() {
            public void windowOpened(WindowEvent e) {}
            public void windowClosing(WindowEvent e) {
                panel.finish();
            }
            public void windowClosed(WindowEvent e) {}
            public void windowIconified(WindowEvent e) {}
            public void windowDeiconified(WindowEvent e) {}
            public void windowActivated(WindowEvent e) {}
            public void windowDeactivated(WindowEvent e) { }
        });
        return frame;
    }
}
